<?php

namespace app\admin\controller;


use app\common\model\TalentListConfModel;
use app\common\model\TopUserModel;
use app\common\model\UserModel;
use think\db\Query;

class TalentController extends BaseController
{
    public function index()
    {
        if ($this->request->isAjax()) {

            if (!in_array($type_0 = $this->get('type_0'), [1, 2])) {
                return json(["count" => 0, "list" => [], "code" => 0, "msg" => ""]);
            }

            if (!in_array($type_1 = (int)$this->get('type_1', 0), [1, 2, 3, 4, 5, 6])) {
                return json(["count" => 0, "list" => [], "code" => 0, "msg" => ""]);
            }

            $order = 'fans';
            if ($type_0 == 2) {
                $order = 'diamond';
            }

            switch ($type_1) {
                case 1:
                    $date = date("Y-m-d",strtotime("-1 day"));
                    $type = 1;
                    break;
                case 2:
                    $date = date('Y-m-d');
                    $type = 1;
                    break;
                case 3:
                    $date = date('Y-m-d', strtotime('-1 sunday', time()));
                    $type = 2;
                    break;
                case 4:
                    $date = date('Y-m-d');
                    $type = 2;
                    break;
                case 5:
                    $date = strtotime(date('Y-m-d', strtotime(date('Y-m-01') . ' -1 day')));
                    $type = 3;
                    break;
                case 6:
                    $date = date('Y-m-d');
                    $type = 3;
                    break;
                default:
                    $type = 1;
                    $date = date('Y-m-d');
            }

            $model = new TopUserModel();
            switch ($type) {
                case 1:
                    $field = 'id,uid,updated_at,date,day_fans_stat as fans,day_diamond_stat as diamond';
                    $model = $model->field($field)->where('date', $date);
                    break;
                case 2:
                    $field = 'id,uid,updated_at,date,week_fans_stat as fans,week_diamond_stat as diamond';
                    $model = $model->field($field)->where('date', $date);
                    break;
                case 3:
                    $field = 'id,uid,updated_at,date,month_fans_stat as fans,month_diamond_stat as diamond';
                    $model = $model->field($field)->where('date', $date);
                    break;
            }

            if ($uid = $this->get('uid', '')) {
                $model = $model->where('uid', $uid);
            }
            return json([
                "count" => $model->count('id'),
                "list" => $model->with(['user' => function (Query $query) {
                    $query->field('id,nick_name,is_anchor,anchor_type');
                }])->page($this->get('page', 1), $this->get('limit', 20))
                    ->order($order, 'desc')
                    ->select()->each(function ($query) use ($type_0, $type) {
                        $query->top_type_0 = $type_0;
                        $query->top_type_1 = $type;
                    })
                    ->toArray(),
                "code" => 0,
                "msg" => ""
            ]);
        }
        return $this->assign(['titles' => json_encode(array_column(TalentListConfModel::lists(), 'title', 'id'))])->fetch();
    }


    public function conf()
    {
        if ($this->request->isAjax()) {
            if (!$id = $this->post('id', '')) {
                return static::bulidFail('ID不能为空');
            }

            if (!$model = TalentListConfModel::where('id', $id)->find()) {
                return static::bulidFail('不存在的配置');
            }

            $model->content = addslashes(trim($this->post('content', '')));
            $model->operator = $this->admin_name;
            if (!$model->save()) {
                return static::bulidFail();
            }

            $logText = $model->id == 1 ?  '主播达人榜配置' : '富豪达人榜配置';
            $this->addAdminLog('修改了' . $logText);

            return static::bulidSuccess();
        }
        return $this->assign(['list' => TalentListConfModel::lists()])->fetch();
    }

    public function edit()
    {
        if ($this->request->isAjax()) {
            if (!$id = $this->post('id')) {
                return static::bulidFail('ID 不能为空');
            }

            if (!$model = TopUserModel::where('id', $id)->find()) {
                return static::bulidFail('该用户不存在达人榜中');
            }

            $type = $this->post('type', 1);

            if ($type == 2) {
                if (ctype_digit($day = $this->post('day'))) {
                    if ($day >= $model->day_diamond_stat) {
                        $model->modify_day_diamond_stat = $day - $model->day_diamond_stat;
                    } else {
                        $model->modify_day_diamond_stat = 0;
                    }
                }

                if (ctype_digit($week = $this->post('week'))) {
                    if ($week >= $model->week_diamond_stat) {
                        $model->modify_week_diamond_stat = $week - $model->week_diamond_stat;
                    } else {
                        $model->modify_week_diamond_stat = 0;
                    }
                }

                if (ctype_digit($month = $this->post('month'))) {
                    if ($month >= $model->month_diamond_stat) {
                        $model->modify_month_diamond_stat = $month - $model->month_diamond_stat;
                    } else {
                        $model->modify_month_diamond_stat = 0;
                    }
                }
            } else {
                if (ctype_digit($day = $this->post('day'))) {
                    if ($day >= $model->day_fans_stat) {
                        $model->modify_day_fans_stat = $day - $model->day_fans_stat;
                    } else {
                        $model->modify_day_fans_stat = 0;
                    }
                }

                if (ctype_digit($week = $this->post('week'))) {
                    if ($week >= $model->week_fans_stat) {
                        $model->modify_week_fans_stat = $week - $model->week_fans_stat;
                    } else {
                        $model->modify_week_fans_stat = 0;
                    }
                }

                if (ctype_digit($month = $this->post('month'))) {
                    if ($month >= $model->month_fans_stat) {
                        $model->modify_month_fans_stat = $month - $model->month_fans_stat;
                    } else {
                        $model->modify_month_fans_stat = 0;
                    }
                }
            }

            $model->operator = $this->admin_name;

            if (!$model->save()) {
                return static::bulidFail();
            }

            return static::bulidSuccess();

        }
        if (!$id = $this->get('id')) {
            return static::bulidFail('ID 不能为空');
        }

        if (!$model = TopUserModel::where('id', $id)->find()) {
            return static::bulidFail('该用户不存在达人榜中');
        }

        $model->type = $this->get('type', 1);
        $model->nick_name = $model->user->nick_name;
        if ($model->type == 2) {
            $model->day = $model->day_diamond_stat + $model->modify_day_diamond_stat;
            $model->week = $model->week_diamond_stat + $model->modify_week_diamond_stat;
            $model->month = $model->month_diamond_stat + $model->modify_month_diamond_stat;
        } else {
            $model->day = $model->day_fans_stat + $model->modify_day_fans_stat;
            $model->week = $model->week_fans_stat + $model->modify_week_fans_stat;
            $model->month = $model->month_fans_stat + $model->modify_month_fans_stat;
        }

        return $this->assign([
            'model' => $model,
            'list' => TalentListConfModel::lists()
        ])->fetch();
    }

    public function add()
    {
        if ($this->request->isAjax()) {
            if (!$uid = $this->post('uid')) {
                return static::bulidFail('ID 不能为空');
            }

            if (TopUserModel::field('id')->where('uid', $uid)->find()) {
                return static::bulidFail('请勿重新添加');
            }

            if (!$user = UserModel::field('id')->where('id', $uid)->find()) {
                return static::bulidFail('该用户不存在');
            }

            if ($user->status == 2) {
                return static::bulidFail('该用户已封禁');
            }

            if ($user->status == 40) {
                return static::bulidFail('该用户已注销');
            }

            $model = new TopUserModel();
            $model->uid = $uid;
            $model->date = date('Y-m-d');

            $type = $this->post('type', 1);
            if ($type == 2) {
                if (ctype_digit($day = $this->post('day'))) {
                    $model->modify_day_diamond_stat = $day;
                }

                if (ctype_digit($week = $this->post('week'))) {
                    $model->modify_week_diamond_stat = $week;
                }

                if (ctype_digit($month = $this->post('month'))) {
                    $model->modify_month_diamond_stat = $month;
                }
            } else {
                if (ctype_digit($day = $this->post('day'))) {
                    $model->modify_day_fans_stat = $day;
                }

                if (ctype_digit($week = $this->post('week'))) {
                    $model->modify_week_fans_stat = $week;
                }

                if (ctype_digit($month = $this->post('month'))) {
                    $model->modify_month_fans_stat = $month;
                }
            }

            $model->operator = $this->admin_name;

            if (!$model->save()) {
                return static::bulidFail();
            }

            return static::bulidSuccess();

        }

        return $this->assign([
            'list' => TalentListConfModel::lists()
        ])->fetch();
    }
}